package com.example.common.recycler.viewbinding;

import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewbinding.ViewBinding;

import com.example.common.recycler.base.BaseBindView;
import com.example.common.recycler.base.BindViewStore;
import com.example.common.recycler.base.IBIndViewManager;

import java.lang.reflect.Method;
import java.util.List;

/**
 * 相比较ViewBindingBindViewManager2，利用反射内部进行了绑定调用，因此BaseViewBindingBindView对用户来说更加简洁
 * 注意：
 * 请务必在混淆中增加如下已保证inflate方法不被混淆：
 * -keepclasseswithmembers class **.databinding.** {
 * public static ** inflate(**);
 * }
 *
 * @param <T>
 * @param <VB>
 */
public class ViewBindingBindViewManager<T, VB extends ViewBinding> implements IBIndViewManager<T, ViewBindingViewHolder<VB>, BaseViewBindingBindView<T, VB>> {
    private BindViewStore<BaseViewBindingBindView<T, VB>> bindViewStore = new BindViewStore<>();
    //<viewtype,hashcode>
    private SparseArray<T> bindViewHashCode = new SparseArray<>();

    @Override
    public ViewBindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        T item = bindViewHashCode.get(viewType);
        int hashcode = getHashCode(item);
        BaseViewBindingBindView baseViewBindingBindView = bindViewStore.getBindView(hashcode);
//        Log.i("123456","viewBindingType:"+baseViewBindingBindView.viewBindingType);
        //利用反射进行bing操作
        VB result = null;
        try {
            //由于以下使用到反射，因此反射方法必须不能被混淆
            Class cls = (Class) baseViewBindingBindView.viewBindingType;
            Method bindMethod = cls.getMethod("inflate", LayoutInflater.class, ViewGroup.class, boolean.class);
            result = (VB) bindMethod.invoke(null, LayoutInflater.from(parent.getContext()), parent, false);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("发生错误：" + e.getMessage() + ",请确保填写的泛型（" + baseViewBindingBindView.viewBindingType + "）无误或者在混淆中配置使得自动生成的databinding类的inflate方法不被混淆：\n-keepclasseswithmembers class **.databinding.** {\n" +
                    "    public static ** inflate(android.view.LayoutInflater,android.view.ViewGroup,boolean);\n" +
                    "}");
        }
        return new ViewBindingViewHolder(result);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewBindingViewHolder<VB> holder, int position, @NonNull T t, @NonNull List<Object> payloads) {
        int type = getHashCode(t);
        bindViewStore.getBindView(type).bindViewData(holder, position, t, payloads);
    }

    @Override
    public void onViewRecycled(@NonNull ViewBindingViewHolder<VB> holder, @NonNull T t) {
        int code = getHashCode(t);
        BaseBindView bindView = bindViewStore.getBindView(code);
        if (bindView == null) {
            return;
        }
        bindView.onViewRecycled(holder);
    }

    @Override
    public void register(@NonNull BaseViewBindingBindView bindView) {
        bindViewStore.putBindView(bindView.itemBeanType.hashCode(), bindView);
    }

    @Override
    public int getItemViewType(int position, @NonNull T t) {
        int type = t.getClass().hashCode();
        int resId = bindViewStore.getBindView(type).viewBindingType.hashCode();
        bindViewHashCode.put(resId, t);
        return resId;
    }

    private int getHashCode(@NonNull T t) {
        return t.getClass().hashCode();
    }
}